define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Distributor_add,GoDialog, GoAddress,GoCatTypeAuth,Auth,$window,$ionicModal,$stateParams,$timeout,GoPage,GoBrandAddress) {
		$scope.cats = new Array(); //显示的类目
		$scope.regions = new Array(); //显示的地区
		$scope.input = new Object();
		$scope.input.agenter = null;
		$scope.input.cat_type = new Array(); //提交的结果{id:"001"}
		$scope.input.region = new Array(); //提交的结果{id:"001"}
		$scope.input.re = new Array();
		$scope.input.is_price = false; //是否可以改价
		$scope.input.credit_line = 0; //授信额度
		$scope.input.key = '';
		$scope.input.nPage = 1;
		$scope.input.moreArray = new Array(1); //控制品牌、品类、区域、能否改价的个数
		$scope.scope = new Object();
		$scope.scope.key = '';
		$scope.scope.region = '';
		$scope.angeter_name = '';
		$scope.angeter_id = '';
		$scope.is_reduce = false;
		$scope.is_reduce = false;
			$scope.store_id = $stateParams.store_id;
		$scope.$on('$ionicView.afterEnter', function() {
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
		}, false);
		$scope.init_data = function() {
			$scope.input.nPage = 1;
			$scope.do_agentSearch();
			$scope.bind_region('input.province', 'input.city', 'input.district');
			if($scope.store_id != '') {
				$scope.title = "修改经销商";
				Distributor_add.agenter_detail($scope.store_id, function(data) {
					$scope.detail = data;
//					console.log(data);
					$scope.agenter_name = $scope.detail.store_name;
					$scope.agenter_id = $scope.detail.store_id;
					$scope.input.credit_line = $scope.detail.credit_line;
					$scope.detail.credit_line = parseFloat($scope.detail.credit_line) ;
					$scope.init_brand();
					for(var i = 0; i < $scope.detail.brand.length; i++) {
						$scope.detail.brand[i].brand= null;
						$scope.detail.brand[i].cat1 = null;
						$scope.detail.brand[i].cat2 = null;
						$scope.detail.brand[i].cat3 = null;
						$scope.detail.brand[i].pronvice = null;
						$scope.detail.brand[i].city = null;
						$scope.detail.brand[i].district = null;
						$scope.bind_cat('detail.brand[' + i + '].brand', 'detail.brand[' + i + '].cat1', 'detail.brand[' + i + '].cat2', 'detail.brand[' + i + '].cat3', i);
						$scope.bind_region('detail.brand[' + i + '].province', 'detail.brand[' + i + '].city', 'detail.brand[' + i + '].district', i);
					}
				})
			} else {
				$scope.title = "添加经销商";
				$scope.detail = new Object();
				$scope.detail.brand = new Array();
				$scope.detail.credit_line = 0;
				$scope.detail.brand[0] = {
					"brand": null,
					"cat1":null,
					"cat2":null,
					"cat3":null,
					"pronvice":null,
					"city":null,
					"district":null,
					"is_price":false,
					"cat_type":new Array(),
					"region":new Array()
				}
				$scope.bind_cat('detail.brand[' + 0 + '].brand', 'detail.brand[' + 0 + '].cat1', 'detail.brand[' + 0 + '].cat2', 'detail.brand[' + 0 + '].cat3', 0);
				$scope.bind_region('detail.brand[' + 0 + '].province', 'detail.brand[' + 0 + '].city', 'detail.brand[' + 0 + '].district', 0);
			}

		};

		/***省市区三级联动***/
		$scope.input.province = null; //省市区三级联动
		$scope.input.city = null; //省市区三级联动
		$scope.input.district = null; //省市区三级联动
		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function(data, id) {
			if(id != undefined) {
				if($scope.is_reduce==false){
					$scope.detail.brand[id].city_items = data;
				$scope.detail.brand[id].district_items = null;
				$scope.detail.brand[id].city = null;
				$scope.detail.brand[id].district = null;
				}
				
			} else {
				$scope.input.city = null; //省市区三级联动
				$scope.input.district = null; //省市区三级联动
			}

		};
		$scope.listen_city = function(data, id) {
			if(id != undefined) {
				if($scope.is_reduce==false){
					$scope.detail.brand[id].district_items = data;
				}
				
			} else
				$scope.input.district = null;
		};
		$scope.doSelectProvince = function(detail){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",detail.province_items,detail.province,function(data){
				detail.province = data;
			});
		}
		$scope.doSelectCity = function(detail){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",detail.city_items,detail.city,function(data){
				detail.city = data;
			});
		}
		$scope.doSelectDistrict = function(detail){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",detail.district_items,detail.district,function(data){
				detail.district = data;
			});
		}
		//定义绑定
		$scope.bind_region = function(ob1, ob2, ob3,n) {
			var items = new Array();
			var obj1 = {
				name: ob1,
				id: n
			};
			var obj2 = {
				name: ob2,
				id: n
			};
			var obj3 = {
				name: ob3,
				id: n
			};
			items.push(obj1);
			items.push(obj2);
			items.push(obj3);
//			GoAddress.init_load($scope, items);
			GoBrandAddress.init_load_brand($scope,items);
		}


		/***分类三级联动***/
		$scope.input.store_id = Auth.get_store_id(); //隐藏的企业id的绑定数据
		$scope.input.brand = null; //这是品牌下拉框的绑定数据
		$scope.input.cat1 = null;
		$scope.input.cat2 = null;
		$scope.input.cat3 = null;
		//下拉侦听
		$scope.listen_brand = function(data, id,is_init) {
			if(id != undefined) {
				if($scope.is_reduce==false){
				$scope.detail.brand[id].cat1_type_items = data;
				$scope.detail.brand[id].cat2_type_items = null;
				$scope.detail.brand[id].cat3_type_items = null;
//				console.log("a:"+id);
//				console.log($scope.detail.brand[id].brand);
				if($scope.detail.brand[id].brand!=null){
//					console.log($scope.detail.brand[id].brand.id);
					GoBrandAddress.init_province($scope,id,$scope.detail.brand[id].brand.id);
				}
				
				if(is_init!=true&&data!=null){
					$scope.detail.brand[id].cat_type = new Array();
					$scope.detail.brand[id].region = new Array();
				}
				
				}
				
			} else {
				$scope.input.cat1 = null;
				$scope.input.cat2 = null;
				$scope.input.cat3 = null;
				$scope.cats = new Array();
			}
		};
		$scope.listen_cat1 = function(data, id) {
			if(id != undefined) {
				if($scope.is_reduce==false){
					$scope.detail.brand[id].cat2_type_items = data;
				$scope.detail.brand[id].cat3_type_items = null;
				}
				
			} else {
				$scope.input.cat2 = null;
				$scope.input.cat3 = null;
			}

		};
		$scope.listen_cat2 = function(data, id) {
			if(id != undefined) {
				if($scope.is_reduce==false){
				$scope.detail.brand[id].cat3_type_items = data;
				$scope.detail.brand[id].cat3 = null;
				}
				
				
			} else
				$scope.input.cat3 = null;
		};
		//定义绑定
		$scope.bind_cat = function(brand, cat1, cat2, cat3, i) {
			var items_cat = new Array();
			var obj_brand = {
				name: brand,
				id: i
			};
			var obj1_cat = {
				name: cat1,
				id: i
			};
			var obj2_cat = {
				name: cat2,
				id: i
			};
			var obj3_cat = {
				name: cat3,
				id: i
			};
			items_cat.push(obj_brand);
			items_cat.push(obj1_cat);
			items_cat.push(obj2_cat);
			items_cat.push(obj3_cat);
			GoCatTypeAuth.init_load($scope, items_cat);
			
		}
		$scope.doSelectbrand = function(detail){
			//id是值 name是显示
			GoDialog.showSelect("品牌",detail.brand_items,detail.brand,function(data){
				detail.brand = data;
			});
		}
		$scope.doSelectCat1 = function(detail){
			//id是值 name是显示
			GoDialog.showSelect("大分类",detail.cat1_type_items,$scope.input.cat1,function(data){
				detail.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(detail){
			//id是值 name是显示
			GoDialog.showSelect("中分类",detail.cat2_type_items,$scope.input.cat2,function(data){
				detail.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(detail){
			//id是值 name是显示
			GoDialog.showSelect("小分类",detail.cat3_type_items,$scope.input.cat3,function(data){
				detail.cat3 = data;
			});
		}
		//添加类目
		$scope.input_cat_data = function(brand) {
			if(brand.cat1 == null) {
				GoDialog.toast("请选择第一级类目");
			} else if(brand.cat2 == null) {
				var names = brand.cat1.name;
				if($scope.check(names, 1,brand)) {
					GoDialog.toast("不能重复添加");
				} else {
					var i= brand.cat_type.length;
					brand.cat_type[i] = {
						"name":names,
						"id":names+':'+brand.cat1.id
					}
				}
			} else if(brand.cat3 == null) {
				//名称拼接
				var names = brand.cat1.name + "/" + brand.cat2.name;
				if($scope.check(names, 1,brand)) {
					GoDialog.toast("不能重复添加");
				} else {
					var i= brand.cat_type.length;
					brand.cat_type[i] = {
						"name":names,
						"id":names+':'+brand.cat2.id
					}
				}
			} else {
				//名称拼接
				var names = brand.cat1.name + "/" + brand.cat2.name + "/" + brand.cat3.name;
				if($scope.check(names, 1,brand)) {
					GoDialog.toast("不能重复添加");
				} else {
					var i= brand.cat_type.length;
					brand.cat_type[i] = {
						"name":names,
						"id":names+':'+brand.cat3.id
					}
				}
			}
		};
		//添加地区
		$scope.input_region_data = function(brand) {
			if(brand.province == null) {
				GoDialog.toast("请选择省份");
				return;
			} else if(brand.city == null) {
				var names = brand.province.name;
				if($scope.check(names, 2,brand)) {
					GoDialog.toast("不能重复添加");
				} else {
//					brand.region.push(names); //显示的
					var i=brand.region.length;
					brand.region[i] = {
						"name": names,
						"id":names+':'+brand.province.id
					}
				}
			} else if(brand.district == null) {
				//名称拼接
				var names = brand.province.name + "/" + brand.city.name;
				if($scope.check(names, 2,brand)) {
					GoDialog.toast("不能重复添加");
				} else {
					var i=brand.region.length;
					brand.region[i] = {
						"name" :names,
						"id":names+':'+brand.city.id
					}
				}
			} else {
				//名称拼接
				var names = brand.province.name + "/" + brand.city.name + "/" + brand.district.name;
				if($scope.check(names, 2,brand)) {
					GoDialog.toast("不能重复添加");
				} else {
					var i=brand.region.length;
					
					brand.region[i] = {
						"name" :names,
						"id":names+':'+brand.district.id
					}
				}
			}
			
		};
		//判断存在性
		$scope.check = function(names, type,brand) {
			if(type == 1) {
				for(var i = 0; i < brand.cat_type.length; i++) {
					if( brand.cat_type[i].name == names) { return true; }
				}
			} else {
				for(var i = 0; i < brand.region.length; i++) {
					if(brand.region[i].name == names) { return true; }
				}
			}
			return false;
		}

		$ionicModal.fromTemplateUrl('templates/modal/add_agent.html', {
			scope: $scope,
		}).then(function(modal) {
			$scope.agentModal = modal;
		});
		$scope.showAgent = function() {
			$scope.uncheck_count = 0;
			$scope.is_add = true;
			$scope.agentModal.show();

		}
		$scope.agenter_checked = function(store_id) {
			for(var i = 0; i < $scope.agenter_list.store.length; i++) {
				if($scope.agenter_list.store[i].store_id != store_id) {
					$scope.agenter_list.store[i].checked = false;
				} else {
					if($scope.agenter_list.store[i].checked == true)
						$scope.agenter_list.store[i].checked = false;
					else
						$scope.agenter_list.store[i].checked = true;
				}

			}
		}
		$scope.closeSpec = function() {
			for(var i = 0; i < $scope.agenter_list.store.length; i++) {
				if($scope.agenter_list.store[i].checked == true) {
					$scope.agenter_name = $scope.agenter_list.store[i].store_name;
					$scope.agenter_id = $scope.agenter_list.store[i].store_id;
					$scope.detail.store_id = $scope.agenter_id;
				} else
					$scope.uncheck_count++;
			}
			if($scope.uncheck_count == $scope.agenter_list.store.length) {
				$scope.agenter_name = '';
				$scope.agenter_id = '';
			}
			$scope.agentModal.hide();
		}
		$scope.do_agentSearch = function() {
			$scope.region = '';
			if($scope.input.province != null) {
				$scope.region = $scope.input.province.id;
			}
			if($scope.input.city != null) {
				$scope.region += $scope.input.city.id;
			}
			if($scope.input.district != null) {
				$scope.region += $scope.input.district.id;
			}
			Distributor_add.agenter_list($scope.input.key, $scope.region, $scope.input.nPage, function(data) {
				if(data != null)
					$scope.agenter_list = data;
				else
					$scope.agenter_list = new Object();
			})
		}
		$scope.$on('$destroy', function() {
			if($scope.agentModal)
				$scope.agentModal.remove();
		});
		//点击其他区域关闭弹出窗       
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {　
			if(event.target.nodeName === 'ION-CONTENT' || event.target.nodeName === 'DIV') {　　　　
				if($scope.agentModal) {　　　
					if($scope.agentModal.isShown())　　　
						$scope.closeSpec();
					$scope.is_add = false;　　　　
				}
			}
		});
		$scope.isLoad = false;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			if($scope.input.province != null) {
				$scope.region = $scope.input.province.id;
			}
			if($scope.input.city != null) {
				$scope.region += $scope.input.city.id;
			}
			if($scope.input.district != null) {
				$scope.region += $scope.input.district.id;
			}
			Distributor_add.agenter_list($scope.input.key, $scope.region, 1, function(data) {
				if(data != null)
					$scope.agenter_list = data;
				else
					$scope.agenter_list = new Object();
				$scope.input.nPage = 1;
			}, function() {
				$scope.$broadcast('srcoll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			})
		}
		$scope.go_myagent = function(goods_id) {
			GoPage.go_states("distributor",{"is_init":true});
		}
		$scope.doRefreshMore = function() {
			if($scope.agenter_list.store == undefined)
				$scope.input.nPage = 0;
			Distributor_add.agenter_list($scope.input.key, $scope.region, $scope.input.nPage + 1, function(data) {
				if(data != null) {
					if($scope.agenter_list.store != undefined) {
						$scope.agenter_list.store = $scope.agenter_list.store.concat(data.store);
						$scope.input.nPage += 1;
					} else {
						$scope.agenter_list = data;
						$scope.input.nPage = 1;
					}
				}

			}, function() {
				$scope.$broadcast('srcoll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			})
		}

		//5-8新增
		$scope.addMore = function() {
			var i = $scope.detail.brand.length;
			$scope.detail.brand[i] = {
				"brand":null,
				"cat1":null,
				"cat2":null,
				"cat3":null,
				"pronvice":null,
				"city":null,
				"district":null,
				"cat_type":new Array(),
				"region":new Array()
			};
			$scope.bind_cat('detail.brand[' + i + '].brand', 'detail.brand[' + i + '].cat1', 'detail.brand[' + i + '].cat2', 'detail.brand[' + i + '].cat3', i);
			$scope.bind_region('detail.brand[' + i + '].province', 'detail.brand[' + i + '].city', 'detail.brand[' + i + '].district', i);
		
		}
		
		$scope.reduceMore = function(index) {
			$scope.is_reduce = true;
			if($scope.detail.brand.length > 1) {
				$scope.detail.brand.splice(index, 1);
			}
			$timeout(function() {
					$scope.is_reduce = false;
				}, 500);
		}
		$scope.get_brand_id = function(brand_id){
			if(brand_id!=undefined){
				$scope.input.brand = new Object();
				$scope.input.brand.id = brand_id;
			}
			
			
		}
		$scope.init_brand = function(){
			GoCatTypeAuth.init_brand($scope,function(data){
				for(var n =0;n<$scope.detail.brand.length;n++){
					for(var i =0;i<$scope.brand_items.length;i++){
					if($scope.detail.brand[n].id.toString()==$scope.brand_items[i].id){
						$scope.detail.brand[n].brand = $scope.brand_items[i];
//						console.log("a:"+i);
//						GoBrandAddress.init_province($scope,n,$scope.brand_items[i].id);
					}
				}
				}
			})
		}
		$scope.get_info = function(){
			$scope.input.brand = new Array();
			if(!$scope.agenter_id) {
				GoDialog.toast("请选择代理商");
				return;
			} else if($scope.detail.brand.length==0){
				GoDialog.toast("选择品牌信息");
					return;
			}else{
				for (var i=0;i<$scope.detail.brand.length;i++) {
				$scope.input.brand[i] = {
					"id":$scope.detail.brand[i].brand.id,
					"region": new Array(),
					"cat_type":new Array(),
					"is_price":$scope.detail.brand[i].is_price
				}
				for(var n=0;n<$scope.detail.brand[i].region.length;n++){
					$scope.input.brand[i].region[n] = {
						"id":$scope.detail.brand[i].region[n].id
					}
				}
				for(var n=0;n<$scope.detail.brand[i].cat_type.length;n++){
					$scope.input.brand[i].cat_type[n] = {
						"id":$scope.detail.brand[i].cat_type[n].id
					}
				}
				if($scope.detail.brand[i].cat_type.length==0){
					GoDialog.toast("请补充完整的分类信息");
					return;
				}
				if($scope.detail.brand[i].region.length==0){
					GoDialog.toast("请补充完整的地域信息");
					return;
				}
			}
			}
			var params = new Object();
			params.brand = $scope.input.brand ;
			params.id = $scope.detail.store_id;
			params.credit_line = $scope.detail.credit_line;
			params = JSON.stringify(params);
			Distributor_add.distributor_add(params, function(data) {
//				console.log(data);
					if(data.success == true) {
						GoDialog.toast("提交成功");
						$scope.go_myagent();
					} else
						GoDialog.toast(data.msg);

				});
		}
		$scope.delete_cat = function(cat,index){
			cat.splice(index,1);
		}
		$scope.delete_region = function(region,index){
			region.splice(index,1);
		}
		
		
	}

	controller.$inject = ['$scope', 'Distributor_add','GoDialog', 'GoAddress','GoCatTypeAuth','Auth','$window','$ionicModal','$stateParams','$timeout','GoPage','GoBrandAddress'];

	return app.registerController("distributor_addctrl",controller);
});